package it.fantacalcio.hibernate.dao;

import it.fantacalcio.model.Role;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

public class RoleDAOImpl extends BaseHibernateDAO implements RoleDAO {

	@Override
	public void save(Role role) throws Exception {
		getSession().save(role);
	}
	
	@Override
	public void delete(Role role) throws Exception {
		getSession().delete(role);
	}
	
	@Override
	public void update(Role role) throws Exception {
		getSession().saveOrUpdate(role);		
	}
	
	@Override
	public Role get(Long id) throws Exception {
		return (Role) getSession().get(Role.class, id);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Role> list() throws Exception {
		String queryString = "from Role";
		Query queryObject = getSession().createQuery(queryString);
		return queryObject.list();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Role> findByExample(Role role) throws Exception {
		Criteria cri = getSession().createCriteria(Role.class);
		cri.add(Example.create(role).ignoreCase());
		List<Role> results = cri.list();
		return results;
	}

}
